import * as usersService from '../services/users';


const paginateConst ={
  current:1,
  defaultCurrent:1,
  total:0,
  defaultPageSize:20,
  pageSize:20,
}


export default {
  namespace: 'users',
  state: {
    list: [],
    pagination: paginateConst,
  },
  reducers: {
    medicine_fetch_successed(state, { payload: { data: list, pagination } }) {
      return { ...state, list, pagination };
    },
  },
  effects: {
    *fetch({ payload: { current } }, { call, put }) {
      console.log("effects page",current)
      const { data, headers } = yield call(usersService.fetch, { current });
      const pagination  =  JSON.parse(headers['pagination'])
      console.log("pagination",pagination)
      yield put({ type: 'medicine_fetch_successed', payload: { data, pagination: pagination} });
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        if (pathname === '/users') {
          dispatch({ type: 'fetch', payload: query });
        }
      });
    },
  },
};
